﻿@{
    ViewData["Title"] = "Index";
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="~/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <form class="layui-form layui-row layui-col-space16">
        <div class="layui-col-md2">
            <div class="layui-input-wrap">
                <input type="text" name="StockOutCode" value="" placeholder="请输入出库单号" class="layui-input" lay-affix="clear" id="workshopCode">
            </div>
        </div>
        <div class="layui-col-md2">
            <div class="layui-input-wrap">
                <select name="StockOutType" lay-filter="Status">
                    <option value="">请选择出库类型</option>
                    <option value="销售提货出库">销售提货出库</option>
                    <option value="采购退货出库">采购退货出库</option>
                    <option value="调拨出库">调拨出库</option>
                </select>
            </div>
        </div>
        <div class="layui-btn-container layui-col-xs12">
            <button class="layui-btn" lay-submit lay-filter="demo-table-search">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="button" class="layui-btn  layui-btn-normal" onclick="add()">新增</button>
        </div>
    </form>
    <table class="layui-hide" id="demo" lay-filter="test">
    </table>
    <script type="text/html" id="demo-tools">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs layui-bg-purple layui-btn-radius" lay-event="detail">查看</a>
             <a class="layui-btn layui-btn-xs layui-bg-orange layui-btn-radius" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-bg-red layui-btn-radius" lay-event="del">删除</a>

        </div>
    </script>

    <script src="~/layui-v2.9.16/layui/layui.js"></script>
    <script src="~/libs/jquery/jquery.js"></script>
    <script>
        layui.use(['table', 'form'], function () {
            var table = layui.table;
            var form = layui.form;

            // 创建渲染实例
            table.render({
                elem: '#demo',
                url: '/Oustorage/GetOuStorages', // 此处为静态模拟数据，实际使用时需换成真实接口
                cols: [[
                    { field: 'stockOutCode', title: '出库单号', width: 150 },
                    { field: 'productName', title: '产品名称', width: 150 },
                    { field: 'productCode', title: '产品编号', width: 150 },
                    { field: 'unit', title: '单位', width: 150 },
                    { field: 'quantity', title: '数量', width: 150 },

                    { field: 'stockInWarehouse', title: '仓库', width: 200 },
                    { field: 'stockInLocation', title: '库位', width: 200 },
                    { field: 'stockOutType', title: '出库类型', width: 200 },
                    { field: 'customerName', title: '客户', width: 200 },
                    { field: 'maker', title: '制单人', width: 200 },
                    { field: 'writes', title: '操作', width: 200, toolbar: "#demo-tools" }
                ]],
                height: 520,
                page: {
                    limit: 5,
                    limits: [5, 10, 15, 20, 25],
                    prev: '上一页',
                    next: '下一页',
                    first: '首页',
                    last: '尾页'
                },
                request: {
                    pageName: 'pageIndex',
                    limitName: 'pageSize'
                },
                parseData: function (res) {
                    // 可进行数据操作
                    console.log(res);
                    return {
                        "code": res.code, //解析接口状态
                        "count": res.count, //解析数据长度
                        "data": res.data//解析数据列表
                    };
                },
            });
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('demo', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                layer.msg('搜索成功');
                return false; // 阻止默认 form 跳转
            });

            //点击工具栏事件
            table.on('tool(test)', function (obj) {
                var data = obj.data; // 获取当前行数据
                var layEvent = obj.event; // 获取 lay-event 对应的值
                if (layEvent === 'del') { // 删除操作
                    layer.confirm('真的删除行么', function (index) {
                        // 向服务端发送删除指令
                        $.ajax({
                            url: "/Oustorage/DeleteOuStorage?StockOutCode=" + data.stockOutCode, // 这里的URL需要根据实际情况进行修改
                            type: 'get',
                            success: function (response) {
                                if (response.data == true) {
                                    // 响应成功，重载表格
                                    layer.msg('删除成功');
                                    table.reload('demo');
                                    layer.close(index);
                                } else {
                                    layer.msg('删除失败');
                                }
                            },
                            error: function () {
                                layer.msg('删除失败');
                            }
                        });
                    });
                }
                if (layEvent === 'edit') {
                    // 打开编辑页面
                    layer.open({
                        type: 2,
                        title: '编辑销售出货',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['800px', '650px'],
                        content: '/ProductSalse/Edit?id=' + data.id,
                        end: function () {
                            table.reload('demo');//刷新父级页面列表方法，类似于回调函数
                        }
                    });
                }
                if (layEvent === 'Ope') {
                    layer.confirm('确定执行出库操作吗？', function (index) {
                        // 向服务端发送退货指令
                        $.ajax({
                            url: "/ProductSalse/UpdateStatus?id=" + data.id, // 这里的URL需要根据实际情况进行修改
                            type: 'get',
                            success: function (response) {
                                if (response.data == true) {
                                    // 响应成功，重载表格
                                    layer.msg('出库成功');
                                    table.reload('demo');
                                    layer.close(index);
                                } else {
                                    layer.msg('出库失败');
                                }
                            },
                            error: function () {
                                layer.msg('出库失败');
                            }
                        });
                    });
                }

            });
        });
        //弹框打开新增页面
        function add() {
           location.href = "/Oustorage/Add";
        }
    </script>
</body>
</html>
